package com.shenfc.fxzj.util;


import com.shenfc.fxzj.domain.Member;
import com.shenfc.fxzj.domain.MemberOauth;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 
 * Cookie 工具类
 *
 */
public final class SessionUtils {
	
	private static final String SESSION_USER = "session_user";

	private static final String SESSION_QQ_USER = "session_qq_user";

	private static final String SESSION_VALIDATECODE = "session_validatecode";//验证码
	
	
	private static final String SESSION_ACCESS_URLS = "session_access_urls"; //系统能够访问的URL
	
	
	private static final String SESSION_MENUBTN_MAP = "session_menubtn_map"; //系统菜单按钮

	private static final String MOBILE_VALIDATECODE ="mobile_validatecode";  //手机验证码

	private static final String SESSION_UUID ="session_uuid";  //手机验证码
	
	/**
	  * 设置session的值
	  * @param request
	  * @param key
	  * @param value
	  */
	 public static void setAttr(HttpServletRequest request,String key,Object value){
		 request.getSession(true).setAttribute(key, value);
	 }
	 
	 
	 /**
	  * 获取session的值
	  * @param request
	  * @param key
	  */
	 public static Object getAttr(HttpServletRequest request,String key){
		 return request.getSession(true).getAttribute(key);
	 }
	 
	 /**
	  * 删除Session值
	  * @param request
	  * @param key
	  */
	 public static void removeAttr(HttpServletRequest request,String key){
		 request.getSession(true).removeAttribute(key);
	 }
	 
	 /**
	  * 设置用户信息 到session
	  * @param request
	  * @param member
	  */
	 public static void setUser(HttpServletRequest request, Member member){
		 request.getSession(true).setAttribute(SESSION_USER, member);
	 }

	/**
	 * 设置QQ用户信息 到session
	 * @param request
	 * @param memberOauth
	 */
	public static void setQQUser(HttpServletRequest request, MemberOauth memberOauth){
		request.getSession(true).setAttribute(SESSION_QQ_USER, memberOauth);
	}
	/**
	 * 从session中获取用户信息
	 * @param request
	 * @return SysUser
	 */
	public static Member getUser(HttpServletRequest request){
		return (Member)request.getSession(true).getAttribute(SESSION_USER);
	}

	/**
	 * 从session中获取用户信息
	 * @param request
	 * @return SysUser
	 */
	public static MemberOauth getQQUser(HttpServletRequest request){
		return (MemberOauth)request.getSession(true).getAttribute(SESSION_QQ_USER);
	}
	/**
	 * 用户登录时进行初始化uuidList操作
	 * @param request
	 * @param uuidList
     */
	public static void setUuidList(HttpServletRequest request,List<String> uuidList){
		request.getSession(true).setAttribute(SESSION_UUID, uuidList);
	}
	/**
	 * 将uuid设置到session的uuidList中
	 * @param request
	 * @param uuid
     */
	public static void setUuid(HttpServletRequest request,String uuid){
		List<String> uuidList=(List<String>) request.getSession(true).getAttribute(SESSION_UUID);
		if(StringUtils.isNotBlank(uuid)){
			uuidList.add(uuid);
		};
		request.getSession(true).setAttribute(SESSION_UUID, uuidList);
	}
	/**
	 * 判断参数uuid在session中的uuidList是否存在
	 * @param request
	 * @param uuid
     * @return
     */
	public static Boolean uuidIsExist(HttpServletRequest request,String uuid){
		List<String> uuidList= (List<String>) request.getSession(true).getAttribute(SESSION_UUID);
		if(uuidList.contains(uuid)) {
			return true;
		}
		return false;
	}
	/**
	 * 从session中移除uuid
	 * @param request
     */
	public static void removeUuid(HttpServletRequest request){
		request.getSession(true).removeAttribute(SESSION_UUID);
	}
	/*public static void setUuid(HttpServletRequest request,String uuid){
		if(uuid!=null && !"".equals(uuid)){
			request.getSession(true).setAttribute(uuid, uuid);
		}
	}
	public static Object getUuid(HttpServletRequest request,String uuid){
		if(uuid!=null && !"".equals(uuid)){
			return request.getSession(true).getAttribute(uuid);
		}
		return null;
	}*/

	 /**
	  * 从session中获取用户信息
	  * @param request
	  * @return SysUser
	  */
	 public static void removeUser(HttpServletRequest request){
		removeAttr(request, SESSION_USER);
	 }

	public static void removeQQUser(HttpServletRequest request){
		removeAttr(request, SESSION_QQ_USER);
	}
	 
	 
	 /**
	  * 设置验证码 到session
	  * @param request
	  */
	 public static void setValidateCode(HttpServletRequest request,String validateCode){
		 request.getSession(true).setAttribute(SESSION_VALIDATECODE, validateCode);
	 }
	 
	 
	 /**
	  * 从session中获取验证码
	  * @param request
	  * @return SysUser
	  */
	 public static String getValidateCode(HttpServletRequest request){
		return (String)request.getSession(true).getAttribute(SESSION_VALIDATECODE);
	 }
	 
	 
	 /**
	  * 从session中获删除验证码
	  * @param request
	  * @return SysUser
	  */
	 public static void removeValidateCode(HttpServletRequest request){
		removeAttr(request, SESSION_VALIDATECODE);
	 }
	 
	 /**
	  * 判断当前登录用户是否超级管理员
	  * @param request
	  * @return
	  */
//	 public static boolean isAdmin(HttpServletRequest request){ //判断登录用户是否超级管理员
//		 SysUser user =  getUser(request);
//		 if(user == null  || user.getSuperAdmin() != SuperAdmin.YES.key){
//			 return false;
//		 }
//		 return true;
//	 }
	 
	 
	 
	 /**
	  * 判断当前登录用户是否超级管理员
	  * @param request
	  * @return
	  */
	 public static void setAccessUrl(HttpServletRequest request,List<String> accessUrls){ //判断登录用户是否超级管理员
		 setAttr(request, SESSION_ACCESS_URLS, accessUrls);
	 }
	 
	 
	 
	 /**
	  * 判断URL是否可访问
	  * @param request
	  * @return
	  */
	 public static boolean isAccessUrl(HttpServletRequest request,String url){ 
		 List<String> accessUrls = (List)getAttr(request, SESSION_ACCESS_URLS);
		 if(accessUrls == null ||accessUrls.isEmpty() || !accessUrls.contains(url)){
			 return false;
		 }
		 return true;
	 }
	 
	 
	 /**
	  * 设置菜单按钮
	  * @param request
	  * @param btnMap
	  */
	 public static void setMemuBtnMap(HttpServletRequest request,Map<String,List> btnMap){ //判断登录用户是否超级管理员
		 setAttr(request, SESSION_MENUBTN_MAP, btnMap);
	 }
	 
	 /**
	  * 获取菜单按钮
	  * @param request
	  */
	 public static List<String> getMemuBtnListVal(HttpServletRequest request,String menuUri){ //判断登录用户是否超级管理员
		 Map btnMap  = (Map)getAttr(request, SESSION_MENUBTN_MAP);
		 if(btnMap == null || btnMap.isEmpty()){
			 return null;
		 }
		 return (List<String>)btnMap.get(menuUri);
	 }

	/**
	 * 设置手机验证码
	 * @param request
	 * @param mobileValidateCode
     */
	public static void setMobileValidatecode(HttpServletRequest request,String mobileValidateCode){
		request.getSession(true).setAttribute(MOBILE_VALIDATECODE,mobileValidateCode);
	}

	/**
	 * 获取手机验证码
	 * @param request
	 * @return
     */
	public static String getMobileValidatecode(HttpServletRequest request){
		return (String)request.getSession(true).getAttribute(MOBILE_VALIDATECODE);
	}

	/**
	 * 从session中移除手机验证码
	 * @param request
     */
	public static void removeMobileValidatecode(HttpServletRequest request){
		removeAttr(request, MOBILE_VALIDATECODE);
	}


//		private static final String SESSION_ACCESS_URLS = "session_access_urls"; //系统能够访问的URL
//		
//		
//		private static final String SESSION_MENUBTN_MAP = "session_menubtn_map"; //系统菜单按钮
	
}